home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 1 of 3 / CHAPTER7 / ENUMRLAN.C next >
C/C++ Source or Header  |  1996-01-28  |  7KB  |  225 lines

  1.  
  2. #include <windows.h>  
  3. #include "EnumRLan.h" 
  4.  
  5.  
  6. #if defined (WIN32)
  7.     #define IS_WIN32 TRUE
  8. #else
  9.     #define IS_WIN32 FALSE
  10. #endif
  11.  
  12. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  13. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  14. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  15.  
  16. HINSTANCE hInst;   // current instance
  17.  
  18. LPCTSTR lpszAppName  = "MyApp";
  19. LPCTSTR lpszTitle    = "My Application"; 
  20.  
  21. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  22.  
  23. typedef struct
  24. {
  25.   HWND hWnd;
  26.   WORD wLangID;
  27. } ENUMLANG;
  28.  
  29. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  30.                       LPTSTR lpCmdLine, int nCmdShow)
  31. {
  32.    MSG      msg;
  33.    HWND     hWnd; 
  34.    WNDCLASS wc;
  35.  
  36.    // Register the main application window class.
  37.    //............................................
  38.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  39.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  40.    wc.cbClsExtra    = 0;                      
  41.    wc.cbWndExtra    = 0;                      
  42.    wc.hInstance     = hInstance;              
  43.    wc.hIcon         = LoadIcon( hInstance, lpszAppName ); 
  44.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  45.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  46.    wc.lpszMenuName  = lpszAppName;              
  47.    wc.lpszClassName = lpszAppName;              
  48.  
  49.    if ( IS_WIN95 )
  50.    {
  51.       if ( !RegisterWin95( &wc ) )
  52.          return( FALSE );
  53.    }
  54.    else if ( !RegisterClass( &wc ) )
  55.       return( FALSE );
  56.  
  57.    hInst = hInstance; 
  58.  
  59.    // Create the main application window.
  60.    //....................................
  61.    hWnd = CreateWindow( lpszAppName, 
  62.                         lpszTitle,    
  63.                         WS_OVERLAPPEDWINDOW, 
  64.                         CW_USEDEFAULT, 0, 
  65.                         CW_USEDEFAULT, 0,  
  66.                         NULL,              
  67.                         NULL,              
  68.                         hInstance,         
  69.                         NULL               
  70.                       );
  71.  
  72.    if ( !hWnd ) 
  73.       return( FALSE );
  74.  
  75.    ShowWindow( hWnd, nCmdShow ); 
  76.    UpdateWindow( hWnd );         
  77.  
  78.    while( GetMessage( &msg, NULL, 0, 0) )   
  79.    {
  80.       TranslateMessage( &msg ); 
  81.       DispatchMessage( &msg );  
  82.    }
  83.  
  84.    return( msg.wParam ); 
  85. }
  86.  
  87.  
  88. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  89. {
  90.    WNDCLASSEX wcex;
  91.  
  92.    wcex.style         = lpwc->style;
  93.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  94.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  95.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  96.    wcex.hInstance     = lpwc->hInstance;
  97.    wcex.hIcon         = lpwc->hIcon;
  98.    wcex.hCursor       = lpwc->hCursor;
  99.    wcex.hbrBackground = lpwc->hbrBackground;
  100.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  101.    wcex.lpszClassName = lpwc->lpszClassName;
  102.  
  103.    // Added elements for Windows 95.
  104.    //...............................
  105.    wcex.cbSize = sizeof(WNDCLASSEX);
  106.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  107.                             IMAGE_ICON, 16, 16,
  108.                             LR_DEFAULTCOLOR );
  109.             
  110.    return RegisterClassEx( &wcex );
  111. }
  112.  
  113.  
  114. BOOL CALLBACK EnumLoadMenu( HANDLE hModule, LPCTSTR lpszType,
  115.                             LPCTSTR lpszName, WORD wIDLanguage,
  116.                             LONG lParam )
  117. {
  118.    ENUMLANG* pEnumLang = (ENUMLANG*)lParam;
  119.  
  120.    // Check to see if the language is the right one.
  121.    //...............................................
  122.    if ( pEnumLang->wLangID == wIDLanguage )
  123.    {
  124.       HRSRC res = FindResourceEx( hModule, lpszType, lpszName, wIDLanguage );
  125.  
  126.       // If we were able to find the resource and it is a menu,
  127.       // then load the menu and set it as the main menu of the application.
  128.       //...................................................................
  129.       if ( res && lpszType == RT_MENU )
  130.       {
  131.          HGLOBAL hmem = NULL;
  132.  
  133.          hmem = LoadResource( hModule, res );
  134.          if ( hmem )
  135.          {
  136.             LPVOID  lpmenu;
  137.             HMENU   hmenu;
  138.  
  139.             lpmenu = LockResource( hmem );
  140.  
  141.             hmenu = LoadMenuIndirect( (CONST MENUTEMPLATE*) lpmenu );
  142.             SetMenu( pEnumLang->hWnd, hmenu );
  143.             DrawMenuBar( pEnumLang->hWnd );
  144.  
  145.             return( FALSE );
  146.          }
  147.       }
  148.    }
  149.  
  150.    return( TRUE );
  151. }
  152.  
  153.  
  154. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  155. {
  156.    switch( uMsg )
  157.    {
  158.       case WM_COMMAND :
  159.               switch( LOWORD( wParam ) )
  160.               {
  161.                  case IDM_TEST :
  162.                         {
  163.                            ENUMLANG enumLang;
  164.  
  165.                            enumLang.hWnd = hWnd;
  166.                            enumLang.wLangID = MAKELANGID( LANG_SPANISH,
  167.                                                SUBLANG_SPANISH_MEXICAN );
  168.  
  169.                            if ( EnumResourceLanguages( hInst, RT_MENU,
  170.                                                        lpszAppName, 
  171.                                                        EnumLoadMenu,
  172.                                                        (LONG)&enumLang ) )
  173.                            {
  174.                               MessageBox( hWnd, "Menu could not be found!",
  175.                                                 "EnumLoadMenu",
  176.                                                 MB_OK | MB_ICONASTERISK );
  177.                            }
  178.                         }
  179.                         break;
  180.  
  181.                  case IDM_ABOUT :
  182.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  183.                         break;
  184.  
  185.                  case IDM_EXIT :
  186.                         DestroyWindow( hWnd );
  187.                         break;
  188.               }
  189.               break;
  190.       
  191.       case WM_DESTROY :
  192.               PostQuitMessage(0);
  193.               break;
  194.  
  195.       default :
  196.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  197.    }
  198.  
  199.    return( 0L );
  200. }
  201.  
  202.  
  203. LRESULT CALLBACK About( HWND hDlg,           
  204.                         UINT message,        
  205.                         WPARAM wParam,       
  206.                         LPARAM lParam)
  207. {
  208.    switch (message) 
  209.    {
  210.        case WM_INITDIALOG: 
  211.                return (TRUE);
  212.  
  213.        case WM_COMMAND:                              
  214.                if (   LOWORD(wParam) == IDOK         
  215.                    || LOWORD(wParam) == IDCANCEL)    
  216.                {
  217.                        EndDialog(hDlg, TRUE);        
  218.                        return (TRUE);
  219.                }
  220.                break;
  221.    }
  222.  
  223.    return (FALSE); 
  224. }
  225.